/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.repository;

import me.zhengjie.modules.anniversary.domain.Message;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author Jiayin Wei
* @date 2020-07-27
**/
public interface MessageRepository extends JpaRepository<Message, Long>, JpaSpecificationExecutor<Message> {
    /**
     * 查询留言板信息
     * @param pageable
     * @return
     */
    @Query(value = "SELECT m.id,m.name,m.create_time,m.user_id,m.issue_imags,m.user_type,m.content,m.create_by,u.username,u.nick_name,u.gender,u.avatar_name FROM gzmu_ann_message m LEFT JOIN sys_user u" +
            " ON m.user_id=u.user_id WHERE m.is_enable=1 and m.verify_status=1 order by id DESC",nativeQuery = true)
    List<Map<String,Object>> findMessage(Pageable pageable);

    /**
     * 查询留言板动态信息1
     * @param issueType
     * @return
     */
    @Query(value = "SELECT * FROM gzmu_ann_message WHERE issue_type=:issueType and is_enable=1 and verify_status=1 order by id DESC",nativeQuery = true)
    List<Message> findMessage1(Integer issueType, Pageable pageable);


    /**
     * 添加留言板（根据用户id）
     */
    @Transactional
    @Modifying  //设置当前方法自定义执行的sql语句是INSERT语句，而不是select语句
    @Query(value = " INSERT INTO gzmu_ann_message(name,user_id,issue_type,issue_imags,user_type,content,verify_status,create_by) VALUES(:#{#message.name},:#{#message.userId},:#{#message.issueType},:#{#message.issueImags},:#{#message.userType},:#{#message.content},:#{#message.verifyStatus},:#{#message.createBy}) ",nativeQuery = true)
    int insertMessage1(Message message);

    /**
     * 点赞
     */

}
